using LocalOverride.Fiddler.Store;
using LocalOverride.Fiddler.UrlMapping;
using NSubstitute;
using NUnit.Framework;

namespace LocalOverride.Tests.Store
{
    [TestFixture]
    public class SpecialCaseMappingProviderTests
    {
        [Test]
        public void WillReturnNoResultWhereThereIsNoMappingInTheStore()
        {
            var entry = Substitute.For<MappingEntry>();

            var specialCaseProvider = new SpecialCaseMappingsProvider(entry);

            var result = specialCaseProvider.GetMappingResult("http://blah.com");

            Assert.AreEqual(MappingResult.NoMapping, result);
        }

        [Test]
        public void WillReturnSpecialCaseFromStore()
        {
            var entry = Substitute.For<MappingEntry>();

            const string specialUrl = "http://blah.com?dd=3";
            const string specialLocalPath = @"some special file location";
            entry.SpecialCaseMappings = new SpecialCaseMappingList { new SpecialCaseMapping { LocalPath = specialLocalPath, Url = specialUrl } };
            var specialCaseProvider = new SpecialCaseMappingsProvider(entry);

            var result = specialCaseProvider.GetMappingResult(specialUrl);
            
            Assert.IsTrue(result.HasMatch);
            Assert.AreEqual(specialLocalPath, result.LocalFile);
        }

        [Test]
        public void WillAddUnderscore1ForTheFirstEntry()
        {
            var entry = Substitute.For<MappingEntry>();

            const string specialUrl = "http://blah.com/scriptresource.axd?id=22";
            const string specialLocalPath = @"c:\temp\scriptresource.axd";
            entry.SpecialCaseMappings = new SpecialCaseMappingList { new SpecialCaseMapping { LocalPath = specialLocalPath, Url = specialUrl } };
            var specialCaseProvider = new SpecialCaseMappingsProvider(entry);

            var result = specialCaseProvider.MakeSpecialCase(specialUrl, specialLocalPath);

            Assert.AreEqual(@"c:\temp\scriptresource_1.axd", result);
        }

        [Test]
        public void WillAddUnderscore3WhereFirstTwoMappingsExist()
        {
            var entry = Substitute.For<MappingEntry>();

            const string specialUrl = "http://blah.com/scriptresource.axd?id=22";
            const string specialLocalPath = @"c:\temp\scriptresource.axd";
            entry.SpecialCaseMappings = new SpecialCaseMappingList
                                            {
                                                new SpecialCaseMapping { LocalPath = @"c:\temp\scriptresource_1.axd", Url = specialUrl },
                                                new SpecialCaseMapping { LocalPath = @"c:\temp\scriptresource_2.axd", Url = specialUrl }
                                            };
            var specialCaseProvider = new SpecialCaseMappingsProvider(entry);

            var result = specialCaseProvider.MakeSpecialCase(specialUrl, specialLocalPath);

            Assert.AreEqual(@"c:\temp\scriptresource_3.axd", result);
        }

        [Test]
        public void WillAddSpecialCaseToTheEntry()
        {
            var entry = Substitute.For<MappingEntry>();

            const string specialUrl = "http://blah.com/scriptresource.axd?id=22";
            const string specialLocalPath = @"c:\temp\scriptresource.axd";
            entry.SpecialCaseMappings = new SpecialCaseMappingList
                                            {
                                                new SpecialCaseMapping { LocalPath = @"c:\temp\scriptresource_1.axd", Url = specialUrl },
                                                new SpecialCaseMapping { LocalPath = @"c:\temp\scriptresource_2.axd", Url = specialUrl }
                                            };
            var specialCaseProvider = new SpecialCaseMappingsProvider(entry);

            specialCaseProvider.MakeSpecialCase(specialUrl, specialLocalPath);

            CollectionAssert.Contains(entry.SpecialCaseMappings, 
                new SpecialCaseMapping{ LocalPath = @"c:\temp\scriptresource_3.axd", Url = specialUrl }
                );
            
        }
    }
}